<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class home extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	  public function __construct()
		{
			parent::__construct();
			$this->load->helper('url');
			$this->load->helper('form');
			$this->load->helper('html');
			$this->load->library('email');
			$this->load->library('form_validation');
			$this->load->library('session');
			$this->load->library('pagination');
			$this->load->helper('date');
			$this->load->helper('string');
			$this->load->library('pagination');
			$this->load->model("model_home");
			$this->check_logged();
    }
	
	public function index()
	{
		$data['user_info'] = $this->session->userdata("logged_in");
		$data['all_cat'] = $this->model_home->get_category();
		$data['inc_page'] = "home/dashboard";
		$this->load->view('home',$data);
	}
	
	function signin(){
		$this->form_validation->set_rules('email_log', 'Email', 'required|valid_email');
		$this->form_validation->set_rules('password_log', 'Password', 'required|callback_check_database');
		if ($this->form_validation->run() == FALSE)
		{
			$this->load->view('user_online');
		}else{
			
				redirect("home/online");
			}
		
		}
		
	function check_database($password)
	 {
	   //Field validation succeeded.&nbsp; Validate against database
	   $email = $this->input->post('email_log');
	
	   //query the database
	   
	   $result = $this->model_home->check_signin($email,$password);
	
	   if($result)
	   {
		 $sess_array = array();
		 foreach($result as $row)
		 {
		   $sess_array = array(
			 'id' => $row->user_id,
			 'fname' => $row->user_fname,
			 'lname' => $row->user_lname,
			 'email' => $row->user_email
		   );
		   $this->session->set_userdata('logged_in', $sess_array);
		 }
		 return TRUE;
	   }
	   else
	   {
		 $this->form_validation->set_message('check_database', 'Invalid email or password');
		 return false;
	   }
	 }
	 
	function online(){
		 $user_id = $this->session->userdata("logged_in");
		 $this->model_home->reset_($user_id['id']);
		 $data['user_info'] = $this->session->userdata("logged_in");
		 $this->load->view('online',$data);
		 }
	
	public function putUser(){
		$user_id = $this->session->userdata("logged_in");
		$this->model_home->put_user_online($user_id);
		}
		
	function getUser(){
		$user_id = $this->session->userdata("logged_in");
		$data["user_online"] = $this->model_home->get_user_online($user_id);
		$this->load->view("online_list",$data);
		}
		
	function play(){
		$user_id = $this->session->userdata("logged_in");
		$data["user_online"] = $this->model_home->put_play_on($user_id);
		}
		
	function search_matched(){
		$user_id = $this->session->userdata("logged_in");
		$query = $this->model_home->search_user_matched($user_id);
		$result_array = array();
		if($query->num_rows() == 1){
			$found = $query->row();
			array_push($result_array,true,$found->mat_fp);
			$this->session->set_userdata("opponent_id",$found->mat_fp);
			}else array_push($result_array,false);
		
		echo json_encode($result_array);
		}
	function create_matched(){
		$user_id = $this->session->userdata("logged_in");
		$result_array = array();
		// select id play = 1
		$query = $this->model_home->select_user_play($user_id['id']);
		$play_id = $query->user_id;
		
		if($play_id != ""){
			$cat_id = 1;
			$limit = 5;
			// select question for match by category
			$questions = $this->model_home->select_questions($cat_id,$limit);
			$qTr = "";
			foreach($questions->result() as $row){
				$qTr .= $row->ques_id.",";
				}
			
			// create match, insert play id to table match
			$this->model_home->match_setup($user_id['id'], $play_id, $qTr);
			
			// change play user to not play. update play id to 0
			$this->model_home->update_user_play($user_id['id'], $play_id);
			
			array_push($result_array,true,$play_id);
			$this->session->set_userdata("opponent_id",$play_id);
		}else{
			array_push($result_array,false);
			}
		echo json_encode($result_array);
		}
		
	function match(){
		$data['user_info'] = $this->session->userdata("logged_in");
		$data['opponent_info'] = $this->model_home->get_user_detail($this->session->userdata("opponent_id")); 
		$this->load->view('front_template',$data);
		}
	
	function setReady(){
		$user_id = $this->session->userdata("logged_in");
		$this->model_home->set_user_ready($user_id['id']);
		}
	
	function isReady(){
		$check_ready = $this->model_home->check_user_ready($this->session->userdata("opponent_id"));
		echo json_encode(array("isReady"=>$check_ready));
		}
		
	function test_get_q(){
		$questions = $this->model_home->select_questions(1,5);
			$qTr = "";
			foreach($questions->result() as $row){
				$qTr .= $row->ques_id.",";
				}
			echo $qTr;
		}
	function logout(){
		$this->session->sess_destroy();
		redirect("login");
		}
	function check_logged(){
			if(!$this->session->userdata("logged_in")){
				redirect("login");
				}
		}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */